package com.jianbing.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * Created by Han, Tixiang on 2016/8/19 0019.
 */
public class PropertiesUtils {


    private static Properties properties = null;

    private static Properties load() {

        Properties properties = new Properties();

        String fileName = System.getProperty("config");
        System.out.println("-----------------" + fileName);
//        if (StringUtils.isEmpty(fileName)) {
//            String fileName = "/root/dev/kaifa.properties";
//            String fileName = "c:\\kaifa.properties";
//        }

        try {
            File file = new File(fileName);
            if (!file.exists()) {
                throw new Exception(file.getAbsolutePath());
            }
            properties.load(new FileInputStream(file));
        } catch (Exception e) {
        }

        return properties;
    }

    public static String getProperties(String key) {
        return getProperties(key, null);
    }

    public static String getProperties(String key, String defaulValue) {
        if (properties == null) {
            synchronized (PropertiesUtils.class) {
                if (properties == null) {
                    properties = load();
                }
            }
        }
        return properties.getProperty(key, defaulValue);
    }

    public static void main(String[] args) {

        System.out.println(getProperties("logPath"));
    }
}
